const formatTime = date => {
	let year = date.getFullYear()
	let month = date.getMonth() + 1
	let day = date.getDate()
	let hour = date.getHours()
	let minute = date.getMinutes()
	let second = date.getSeconds()
	return `${[year,month,day].map(formatNumber).join('-')} ${[hour,minute,second].map(formatNumber).join(':')}`
}
const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : `0${n}`
}
/**
*检查field长度是否符合要求
@param {String} field要检查的字符串
@param {Number} minLen 符合要求的最小长度
@param {String} name 用于提示用户的中文名
*/
function checkLength(field, minLen, name) {
	if (field.length >= minLen) {
		return true
	}
	let message = `${name}不能少于${minLen}个字符`
	if (field.length == 0) {
		message = `${name}不能为空`
	}
	uni.showToast({
		title: message,
		icon: 'none'
	});
	return false
}

/**
 * 为字符串实现ellipsis功能
 * @param {String} value 需要折叠大的字符串
 * @param {Object} len 允许显示的最大长度
 */
function ellipsis(value, len) {
	if (value && value.length > len) {
		return `${value.substr(0,len-3)}...`
	}
	return value
}

/**
 * @param {Array} array 原始数组
 */
function distinct(array) {
	let result = []
	let obj = {}
	for (let i of array) {
		if (!obj[i.toLowerCase()]) {
			result.push(i)
			obj[i.toLowerCase()] = 1
		}
	}
	return result
}

module.exports = {
	formatTime,
	checkLength,
	ellipsis,
	distinct
}
